Skip to content

apps: drop redundant 64-bit casts on tv_sec/tv_nsec arithmetic#3476

Open
xiaoxiang781216 wants to merge 1 commit into
apache:masterfrom
xiaoxiang781216:sched/remove-system-time64
Open

apps: drop redundant 64-bit casts on tv_sec/tv_nsec arithmetic#3476
xiaoxiang781216 wants to merge 1 commit into
apache:masterfrom
xiaoxiang781216:sched/remove-system-time64

Conversation

@xiaoxiang781216
Copy link
Copy Markdown
Contributor

Summary

Follow-up cleanup to the recent time_t/clock_t 64-bit consolidation
(commits "drop CONFIG_SYSTEM_TIME64 conditionals" and
"replace sclock_t with clock_t and drop redundant time_t/off_t casts").

NuttX's time_t is now unconditionally a signed 64-bit type, so the
explicit (uint64_t), (int64_t), and (int) casts that used to be
sprinkled around tv_sec / tv_nsec arithmetic — to avoid 32-bit
multiplication/subtraction overflow back when time_t could be 32-bit —
are no longer needed. This PR drops those now-redundant casts to keep
the timekeeping helpers readable and consistent with the rest of the
recently-cleaned-up code.

Files touched (one-liners each):

  • benchmarks/cyclictest/cyclictest.ctimediff_us()
  • benchmarks/sd_bench/sd_bench_main.cget_time_delta_us()
  • examples/oneshot/oneshot_main.cmaxus computation
  • examples/watchdog/watchdog_main.ccurrent_time_ms (×2)
  • industry/nxmodbus/nxmb_internal.hnxmb_util_clock_ms()
  • netutils/ntpclient/ntpclient.ctimespec2ntp()
  • netutils/ptpd/ptpd.cptp_adjtime()
  • system/dd/dd_main.celapsed accounting
  • testing/drivers/drivertest/drivertest_posix_timer.cget_timestamp()
  • testing/drivers/sd_stress/sd_stress_main.cget_time_delta()
  • testing/sched/getprime/getprime_main.celapsed accounting
  • testing/sched/pthread_mutex_perf/pthread_mutex_perf.ctimespec_avg()

Impact

  • Users: none — purely a readability/cleanup change.
  • Build process: none — same generated code on every supported
    compiler (the dropped casts were already no-ops once time_t
    became 64-bit).
  • Hardware: none.
  • Documentation: none.
  • Security: none.
  • Compatibility: none — time_t/tv_sec/tv_nsec widths are
    unchanged; this PR only removes pleonastic casts.

No behavioural change is intended.

Testing

Build-tested host-side together with the corresponding nuttx-side
time_t/clock_t cleanups; the touched helpers all expand to the
same arithmetic as before because the dropped casts no longer
widen anything.

  • Host: Ubuntu 22.04 x86_64, GCC 11
  • Targets exercised by the changed sources at build time:
    • sim:nsh (covers examples/watchdog, examples/oneshot,
      system/dd, testing/sched/*, benchmarks/cyclictest,
      benchmarks/sd_bench, testing/drivers/*)
    • sim:netnsh (covers netutils/ntpclient, netutils/ptpd,
      industry/nxmodbus)

No new compiler warnings; no functional difference observed in the
benchmark / test outputs before and after the change.

Copilot AI review requested due to automatic review settings May 11, 2026 09:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@cederom
Copy link
Copy Markdown
Contributor

cederom commented May 11, 2026

Thank you @xiaoxiang781216 :-) CI failed, restarted :-)

There is small typo to fix in nuttx-apps/apps/testing/drivers/sd_stress/sd_stress_main.c:137: bufffer ==> buffer

@xiaoxiang781216
Copy link
Copy Markdown
Contributor Author

Thank you @xiaoxiang781216 :-) CI failed, restarted :-)

There is small typo to fix in nuttx-apps/apps/testing/drivers/sd_stress/sd_stress_main.c:137: bufffer ==> buffer

Done, but this patch need merge apache/nuttx#18840 first to pass ci.

cederom
cederom previously approved these changes May 14, 2026
Copy link
Copy Markdown
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @xiaoxiang781216 :-)

Conditional approval assuming that apache/nuttx#18840 is merged and this PR builds fine after that :-)

jerpelea
jerpelea previously approved these changes May 18, 2026
Now that time_t is unconditionally 64-bit (signed int64_t) and the
struct timespec fields tv_sec / tv_nsec are wide enough on their own,
the explicit (uint64_t)/(int64_t)/(int) casts that used to guard the
multiplications and subtractions in *_us / *_ms / *_ns helpers are no
longer needed.  Drop them to keep the timekeeping math readable.

In the same spirit, this commit also normalises the printf-style format
specifiers and casts used to print tv_sec / tv_nsec / tv_usec values.
The prior code was a mix of "%d"/"%u"/"%ld"/"%lu"/"%lld" with matching
(int)/(unsigned long)/(long long) casts; some formats truncated time_t
on 32-bit hosts, others mismatched signedness or width.  Replace all
such cases with the portable POSIX-recommended forms:

  - tv_sec  (time_t,       signed, impl-defined width) -> %jd  + (intmax_t)
  - tv_nsec (long,         signed)                     -> %ld  (no cast)
  - tv_usec (suseconds_t / long)                       -> %ld  (no cast)

Also drop two stale `(FAR const time_t *)&ts.tv_sec` casts that are
unnecessary now that ts.tv_sec is plain time_t.

Arithmetic-cleanup files (existing scope):

  - benchmarks/cyclictest/cyclictest.c:        timediff_us()
  - benchmarks/sd_bench/sd_bench_main.c:       get_time_delta_us()
  - examples/oneshot/oneshot_main.c:           maxus computation
  - examples/watchdog/watchdog_main.c:         current_time_ms (x2)
  - industry/nxmodbus/nxmb_internal.h:         nxmb_util_clock_ms()
  - netutils/ntpclient/ntpclient.c:            timespec2ntp()
  - netutils/ptpd/ptpd.c:                      ptp_adjtime()
  - system/dd/dd_main.c:                       elapsed accounting
  - testing/drivers/drivertest/drivertest_posix_timer.c:
                                               get_timestamp()
  - testing/drivers/sd_stress/sd_stress_main.c:get_time_delta()
  - testing/sched/getprime/getprime_main.c:    elapsed accounting
  - testing/sched/pthread_mutex_perf/pthread_mutex_perf.c:
                                               timespec_avg()

Printf-format-fix files (new in this revision):

  - examples/adjtime/adjtime_main.c
  - examples/charger/charger_main.c
  - examples/netpkt/netpkt_ethercat.c
  - fsutils/mkfatfs/mkfatfs.c
  - graphics/tiff/tiff_initialize.c
  - netutils/ptpd/ptpd.c
  - nshlib/nsh_timcmds.c
  - system/coredump/coredump.c
  - system/ptpd/ptpd_main.c
  - testing/drivers/drivertest/drivertest_oneshot.c
  - testing/mm/kasantest/kasantest.c
  - testing/ostest/semtimed.c
  - testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
  - testing/sched/timerjitter/timerjitter.c
  - testing/testsuites/kernel/time/cases/clock_test_clock01.c
  - testing/testsuites/kernel/time/cases/clock_test_smoke.c

No behavioural change.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
@xiaoxiang781216 xiaoxiang781216 dismissed stale reviews from jerpelea and cederom via 6d7f16d May 19, 2026 08:40
@xiaoxiang781216 xiaoxiang781216 force-pushed the sched/remove-system-time64 branch from 43ab6bf to 6d7f16d Compare May 19, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants